package cz.jm.web.jmlib.client.dialog.login;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import cz.jm.web.jmlib.client.dialog.DialogWidget;
import cz.jm.web.jmlib.client.dialog.items.DialogItem;
import cz.jm.web.jmlib.client.dialog.items.InputBox;
import cz.jm.web.jmlib.client.dialog.items.PasswordBox;
import cz.jm.web.jmlib.client.dialog.validation.validators.RequiredValueValidator;
import cz.jm.web.jmlib.client.resources.Resources;

public class LoginView implements LoginPresenter.Display {
	protected final LoginConstants constants = GWT.create(LoginConstants.class);
	
	private final InputBox usernameInput = new InputBox();
	private final PasswordBox passwordInput = new PasswordBox();

	private final DialogWidget dialog = new DialogWidget();

	public LoginView() {

		addDialogItem(usernameInput, constants.username());
		addDialogItem(passwordInput, constants.password());
		
		dialog.addOkButton();
				
		dialog.setIcon(new Image(Resources.INSTANCE.getKeys()));
	}

	
	private void addDialogItem(DialogItem item, String txt) {
		item.getLabel().setText(txt);
		item.addValidator(RequiredValueValidator.createValidator(item));
		dialog.addItem(item);
	}
	
	@Override
	public String getUsername() {
		return usernameInput.getInput().getValue();
	}

	@Override
	public String getPassword() {
		return passwordInput.getPassword().getValue();
	}

	@Override
	public HasClickHandlers getOk() {
		return dialog.getButton();
	}

	@Override
	public Widget asWidget() {
		return dialog;
	}

	@Override
    public boolean isValid() {
	    return dialog.isValid();
    }
}
